package start.demo01.datatype.json.entity.editor.jsonEditor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import start.demo01.datatype.json.constant.Constant;
import start.demo01.datatype.json.interfaces.JSONEditor;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//保留指定属性
public class JSONReserveEditor extends JSONEditor {

    //copy(*) copy(x,y,z)

    public JSONReserveEditor(String args) {
        super(args);
    }

    {
        name = "RESERVE";
    }

    @Override
    protected void operationObject(JSONObject object, String key) {
        return;
    }

    @Override
    protected void operationArray(JSONArray array, int index) {
        return;
    }

    @Override
    protected void operation(JSONObject object) {
        if (args.equals(Constant.F))
            return;
        Iterator<Map.Entry<String, Object>> iterator = object.entrySet().iterator();
        String[] split = args.split(",");
        List<String> list = Arrays.asList(split);
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            if (!list.contains(entry.getKey()))
                iterator.remove();
        }
    }
}
